Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceData:[],
    pageNum:1,
    pageSize:15,
    company_id:0,
    group_id:0,
    group_name:''
  },

  /**
   * 生命周期函数--监听页面加载
   * 获取分组传递过来的分组ID 
   */
  onLoad: function (options) {
    var data = JSON.parse(options.data);
    var This = this;
    This.setData({
      company_id:data[0]['company_id'],
      group_id:data[0]['group_id'],
      group_name:data[0]['group_name']
    });
    wx.getStorage({
      key: 'project_id',
      success(res) {
        wx: wx.request({
          url: getApp().globalurl + '/device?pageNum=' + This.data.pageNum + '&pageSize=' + This.data.pageSize + '&company_id=' + This.data.company_id + '&group_id=' + This.data.group_id + '&search=',
          header: {
            'content-type': 'application/json',// 默认值
            'projectCode': res.data[0],
            'Authorization': res.data[1]
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            This.setData({
              pageNum: This.data.pageNum + 1,
              deviceData: res.data.data
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: 'none',
              duration: 2000
            })
          },
        })
      }
    });
  },


  location: function (event){
    var location = event.currentTarget.id;
    var data = location.split(",");
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        wx.openLocation({
          latitude: Number(data[1]),
          longitude: Number(data[0]),
          scale: 18
        })
      },
      fail:function (res)
      {
        wx.showToast({
          title: '授权失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  bindViewReal:function(event){
    var id = event.currentTarget.id;
    var name = event.currentTarget.dataset.deviceName;
    wx.navigateTo({
      url: '../real/real?id='+id+'&name='+name,
    })
  },

  bindViewHistory:function(event){
    var id = event.currentTarget.id;
    var data = [{ 'device_id': id, 'group_id': this.data.group_id}];
    wx.navigateTo({
      url: '../history/history?data=' + JSON.stringify(data),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var This = this;
    wx.getStorage({
      key: 'project_id',
      success(res) {
        wx: wx.request({
          url: getApp().globalurl + '/device?pageNum=' + 1 + '&pageSize=' + This.data.pageSize + '&company_id=' + This.data.company_id + '&group_id=' + This.data.group_id + '&search=',
          header: {
            'content-type': 'application/json',// 默认值
            'projectCode': res.data[0],
            'Authorization': res.data[1]
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            wx.showToast({
              title: '加载成功',
              icon: 'success',
              duration: 2000
            });
            This.setData({
              pageNum: 2,
              deviceData: res.data.data
            });
            wx.hideNavigationBarLoading();//隐藏导航栏加载
            wx.stopPullDownRefresh();//停止下拉刷新
          },
          fail: function (res) {
            wx.hideNavigationBarLoading();//隐藏导航栏加载
            wx.stopPullDownRefresh();//停止下拉刷新
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: 'none',
              duration: 2000
            });
            This.setData({
              deviceData: []
            });
          },
        })
      }
    });
  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    });
    var This = this;
    wx.getStorage({
      key: 'project_id',
      success(res) {
        wx: wx.request({
          url: getApp().globalurl + '/device?pageNum=' + This.data.pageNum + '&pageSize=' + This.data.pageSize + '&company_id=' + This.data.company_id + '&group_id=' + This.data.group_id + '&search=',
          header: {
            'content-type': 'application/json',// 默认值
            'projectCode': res.data[0],
            'Authorization': res.data[1]
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            wx.hideLoading();
            if (res.data.data == null) {
              wx.showToast({
                title: '到底了',
                icon: 'none',
                duration: 2000
              });
            } else {
              This.setData({
                pageNum: This.data.pageNum + 1,
                deviceData: This.data.deviceData.concat(res.data.data),
              })
            }
          },
          fail: function (res) {
            wx.hideLoading();
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: 'none',
              duration: 2000
            });
            This.setData({
              deviceData: []
            });
          },
        })
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})